import '../../util/_base_export.dart';
import '../../util/_view_export.dart';

import 'package:flutter_form_builder/flutter_form_builder.dart';

import '../../util/app_util.dart';
import '../../blocs/cart.dart';

class OrderTimePage extends StatefulWidget {
// class OrderTimePage extends StatelessWidget {
  const OrderTimePage({Key? key}) : super(key: key);
  @override
  _OrderTimePageState createState() => _OrderTimePageState();
}

class _OrderTimePageState extends State<OrderTimePage> {
  @override
  initState() {
    super.initState();
  }

  GlobalKey<FormBuilderState> _fbKey = GlobalKey<FormBuilderState>();

  // List<DropdownMenuItem> ll_date_dd = List<DropdownMenuItem>();
  List<DropdownMenuItem> ll_date_dd = List<DropdownMenuItem>.empty(growable :true);
  List<DropdownMenuItem> ll_window_dd = List<DropdownMenuItem>.empty(growable :true);
  // List<DropdownMenuItem> ll_window_dd = List<DropdownMenuItem>();

  @override
  Widget build(BuildContext context) {
    CartBloc _cart = Provider.of<CartBloc>(context, listen: false);

    var lm_options = _cart.ticket['dl_time_options'];
    var _list = lm_options.keys.toList();
    var _options_key;
    print('aaaaaaaa');
    print(lm_options);
    print(_list[0]);
    _options_key = _list[0];

    ValueChanged _onChangedDate = (val) {
      // ll_window_dd.clear();
      // ll_window_dd.add(DropdownMenuItem(
      //   value: 'x',
      //   child: Text('配送时间'),
      // ));
      // _fbKey.currentState.value['dl_window'] = 'x';
      // _fbKey.currentState.save();
      // lm_options[val]['window'].forEach((_e) => ll_window_dd.add(DropdownMenuItem(
      //       value: _e,
      //       child: Text(_e),
      //     )));
      // setState(() {});
    };

    lm_options.entries.forEach((_e1) {
      print(_e1);
      _e1.value['window'].forEach((_e2) {
        ll_date_dd.add(DropdownMenuItem(
          value: _e1.key + '@' + _e2,
          child: Text(_e1.value['desc'] + ' ' + _e2),
        ));
      });

      // ll_window_dd.add(DropdownMenuItem(
      //   value: 'x',
      //   child: Text('配送时间'),
      // ));
    });
    // lm_options[_list[0]]['window'].forEach((_e) => ll_window_dd.add(DropdownMenuItem(
    //       value: _e,
    //       child: Text(_e),
    //     )));

    return Scaffold(
      appBar: AppBar(
        brightness: Brightness.light,
        leading: GestureDetector(
          behavior: HitTestBehavior.opaque,
          onTap: () {
            Navigator.pop(context);
          },
          child: Container(
            child: Icon(
              Icons.chevron_left,
              color: AppColor.blackMain,
              size: 32,
            ),
          ),
        ),
        title: Text(_cart.ticket['is_pickup']==1?'自取时间':'配送时间', style: TextStyle(color: AppColor.blackMain)),
      ),
      body: SingleChildScrollView(
        child: Container(
          // alignment: Alignment.center,
          // height: ScreenUtil().screenHeight,
          // width: ScreenUtil().screenWidth,
          // decoration: BoxDecoration(
          //   image: DecorationImage(
          //     image: AssetImage("assets/login/location_bg.png"),
          //     fit: BoxFit.cover,
          //   ),
          // ),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              FormBuilder(
                key: _fbKey,
                autovalidateMode: AutovalidateMode.always,
                initialValue: {},
                enabled: false,
                child: Column(
                  children: <Widget>[
                    Container(
                      // decoration: AppDecoration.buttonTwo, //
                      margin: EdgeInsets.symmetric(horizontal: 50.w),
                      padding: EdgeInsets.symmetric(
                        horizontal: 10.w,
                      ),
                      child: ButtonTheme(
                        alignedDropdown: true,
                        child: FormBuilderDropdown(
                          name: "dl_date",
                          decoration: InputDecoration(
                            labelText: _cart.ticket['is_pickup']==1?'自取日期':'配送日期', //,AppText.text['login_region_label'],
                          ),
                          hint: Text(_cart.ticket['is_pickup']==1?'自取日期':'配送日期'), //Text(AppText.text['login_region_tooltip']),
                          // validator: (value){
                          //   if((value??'').isEmpty){
                          //     return '配送日期不能为空';
                          //   }
                          //   return null;
                          // },
                          // valueTransformer: (text) => num.tryParse(text),
                          items: ll_date_dd,
                          onChanged: _onChangedDate,
                        ),
                      ),
                    ),
                    // Container(
                    //   // decoration: AppDecoration.buttonTwo, //
                    //   margin: EdgeInsets.symmetric(horizontal: 50.w),
                    //   padding: EdgeInsets.symmetric(
                    //     horizontal: 10.w,
                    //   ),
                    //   child: ButtonTheme(
                    //     alignedDropdown: true,
                    //     child: FormBuilderDropdown(
                    //       allowClear: true,
                    //       attribute: "dl_window",
                    //       decoration: InputDecoration(
                    //         labelText: '配送时间', //AppText.text['login_region_label'],
                    //       ),
                    //       hint: Text('请先选择日期'), //Text(AppText.text['login_region_tooltip']),
                    //       validators: [FormBuilderValidators.required()],
                    //       // valueTransformer: (text) => num.tryParse(text),
                    //       items: ll_window_dd,
                    //     ),
                    //   ),
                    // ),
                  ],
                ),
              ),
              Container(
                padding: const EdgeInsets.symmetric(vertical: 16.0),
                child: TextButton(
                  child: Text('确认'),
                  onPressed: () async {
                    _fbKey.currentState?.save();

                    if ((_fbKey.currentState?.value['dl_date'] ?? '').isNotEmpty) {
                      Map<String, dynamic> lm_param = {};
                      List ll_param = _fbKey.currentState?.value['dl_date'].split('@');
                      lm_param = {
                        'dl_date': ll_param[0],
                        'dl_window': ll_param[1],
                      };

                      print(lm_param);

                      var lv_result = await _cart.checkoutUpdate(lm_param);

                      if (lv_result)
                        Navigator.pop(context);
                      else
                        Navigator.pop(context);
                    } else {
                      print('sss');
                    }
                  },
                ),
              ),
              SizedBox(),
            ],
          ),
        ),
      ),
    );
  }
}
